// pages/new-data/index.js
import {
  request
} from '../../api/index';
import {
  fillAll
} from '../../utils/util';
import {
  DRY,
  hasErr,
  inputValidHandle
} from '../../utils/valid'

const app = getApp()

const form = {
  dryingTemperature: {
    name: 'D1',
    type: '干燥温度',
    value: '',
    err: '',
    desc: DRY['dryingTemperature'].unit
  },
  dryingSpeed: {
    name: 'D2',
    type: '干燥速度',
    value: '',
    err: '',
    desc: DRY['dryingSpeed'].unit
  },
  dryStrength: {
    name: 'D3',
    type: '干燥坯体强度',
    value: '',
    err: '',
    desc: DRY['dryStrength'].unit
  },
  dryMoisture: {
    name: 'D4',
    type: '干燥坯体水分',
    value: '',
    err: '',
    desc: DRY['dryMoisture'].unit
  }
}
Page({

  /**
   * 页面的初始数据
   */
  data: {
    form,
    fields: Object.keys(form),
    canNext: false,
    id: '',
    fromNavigation: 0,
    recordState: app.globalData.editState
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    this.setPageProperty(options);
    this.fetchData();
  },

  /**
   * 设置页面属性
   */
  setPageProperty(options) {
    wx.setNavigationBarTitle({
      title: '4-坯体干燥'
    })
    this.setData({
      id: options.id,
      fromNavigation: options.navigation || 0,
      recordState: app.globalData.editState
    })
  },
  onInput(event) {
    const key = event.currentTarget.dataset.key,
      value = event.detail,
      form = this.data.form;
    const target = DRY[key];
    if (inputValidHandle(value, DRY, key) === false) {
      form[key]['err'] = '数值不正确：' + target.desc;
    } else {
      form[key]['err'] = '';
    }
    form[key]['value'] = String(value);
    this.setData({
      form
    })
  },
  onChange(e) {
    if (hasErr(this.data.form)) {
      wx.showToast({
        title: '存在不合理的数值',
        icon: 'none'
      })
      return
    }
    fillAll(e.detail.value, true) && request.dryAdd(Object.assign({
      collectionRecordId: this.data.id
    }, e.detail.value, {
      id: this.data.form.id
    }), this.nextHandle)
  },
  nextHandle() {
    if (this.data.fromNavigation > 0) {
            wx.navigateBack({
              delta: 1
            })
      return
    }
    wx.redirectTo({
      url: '../glaze/index?id=' + this.data.id
    })

  },
  fetchData() {
    request.dryList({
      collectionRecordIds: this.data.id
    }, (e) => {
      let k = Object.assign({}, this.data.form);
      e[0] && this.data.fields.forEach(item => {
        k[item].value = e[0][item],
          k.id = e[0].id
      })
      this.setData({
        form: k
      })
    })
  },
  toHome() {
    wx.navigateBack({
      delta: 6
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})